package com.marktotop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.marktotop.entity.system.ImageAttachment;
import com.marktotop.entity.system.Log;
import com.marktotop.error.SystemCodeEnum;
import com.marktotop.error.SystemException;
import com.marktotop.mapper.ImageMapper;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.IImageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.marktotop.utils.FdfsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-12
 */
@Service
public class ImageServiceImpl extends ServiceImpl<ImageMapper, ImageAttachment> implements IImageService {


    @Autowired
    private FdfsUtil fdfsUtil;

    @Autowired
    private ImageMapper imageMapper;

    /**
     * 上传图片文件 返回上传地址
     * @param file
     * @return
     */
    @Override
    public String uploadImage(MultipartFile file) throws IOException, SystemException {
        if (file.isEmpty()) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"上传的文件不能为空");
        }
        InputStream inputStream = file.getInputStream();
        //文件的原名称
        long size = file.getSize();
        String originalFilename = file.getOriginalFilename();
        String fileExtName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        String path = fdfsUtil.upfileImage(inputStream, size, fileExtName.toUpperCase(), null);
        //保存图片信息到数据库
        BufferedImage image = ImageIO.read(file.getInputStream());
        //如果image=null 表示上传的不是图片格式
        if (image != null) {
            ImageAttachment imageAttachment = new ImageAttachment();
            imageAttachment.setCreateTime(new Date());
            imageAttachment.setHeight(image.getHeight());
            imageAttachment.setWidth(image.getWidth());
            imageAttachment.setMediaType(fileExtName);
            imageAttachment.setMediaType(file.getContentType());
            imageAttachment.setPath(path);
            imageMapper.insert(imageAttachment);
        }
        return  path;
    }


    /**
     * 删除图片资源
     *
     * @param id
     * @throws SystemException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteImage(Long id) throws SystemException {
        ImageAttachment image = imageMapper.selectById(id);
        if(image == null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"图片不存在");
        }else {
            imageMapper.deleteById(id);
            fdfsUtil.deleteFile(image.getPath());
        }
    }


    /**
     * 分页展示上传的图片资源
     *
     * @param pageNum
     * @param pageSize
     * @param image
     * @return
     */
    @Override
    public ResultBean findImageList(Integer pageNum, Integer pageSize, ImageAttachment image) {
        // 开启分页
        Page<ImageAttachment> page = new Page<>(pageNum,pageSize);
        IPage<ImageAttachment> imagesByPage = imageMapper.getImagesByPage(page,image);
        if (null == imagesByPage){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().message("获取全部上传图片信息成功！")
                .data("total",imagesByPage.getTotal())
                .data("imageList",imagesByPage.getRecords());
    }
}
